package com.qianxp.designparttern.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author qianxp
 */
public class JdkProxy {
    private JdkProxy() {
    }

    public static JdkProxy getInstance() {
        return InnerClass.jdkProxy;
    }


    public Object getProxy(Class clazz) {
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), clazz.getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return method.invoke(clazz.newInstance(),args);
            }
        });
    }


    private static class InnerClass {
        static JdkProxy jdkProxy = new JdkProxy();
    }
}
